package top.zhdev.community.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.zhdev.community.interceptor.LoginTicketInterceptor;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    private LoginTicketInterceptor loginTicketInterceptor;

    @Autowired
    public void setLoginTicketInterceptor(LoginTicketInterceptor loginTicketInterceptor){
        this.loginTicketInterceptor = loginTicketInterceptor;
    }

    // 对登录添加拦截器，使loginTicketInterceptor生效
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginTicketInterceptor)
                // 下面这些资源不生效，其他的资源的会触发拦截，从而调用LoginTicketInterceptor
                // http的每一次请求都会使用一个新的线程，而ThreadLocal只在本次线程中生效，故此，需要不断的拦截
                .excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jpeg");
    }
}
